package com.key.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TimeCaculate {

	public static TimeUnion caculateLatestTimeBlank(List<String> mapConfig) {

		SimpleDateFormat sFormat = new SimpleDateFormat("yyyyMMdd");
		String strDateYMD = sFormat.format(new java.util.Date());
		Date dateTime = new Date();
		long lTimeNow = dateTime.getTime();

		TimeUnion timeUnion = new TimeUnion();
		long minBlankTime = -1;
		try {
			for (int i = 0; i < mapConfig.size(); i++) {
				String[] strTemp = mapConfig.get(i).split("-");
				String strKey = strDateYMD + strTemp[0];
				String strValue = strTemp[1];

				SimpleDateFormat sDateFormat = new SimpleDateFormat(
						"yyyyMMddHHmmss");
				Date dateStart = sDateFormat.parse(strKey);
				long lTimeStart = dateStart.getTime();
				long timeBlank = lTimeStart - lTimeNow;
				if (timeBlank < 0) {
					timeBlank = timeBlank + 24 * 60 * 60 * 1000 * 1000;
					Calendar cc = Calendar.getInstance();
					cc.setTime(dateStart);
					cc.set(Calendar.DAY_OF_MONTH,
							cc.get(Calendar.DAY_OF_MONTH) + 1);
					strKey = sDateFormat.format(cc.getTime());
				}
				if (minBlankTime == -1) {
					minBlankTime = timeBlank;
					timeUnion.setStrTime(strKey);
					timeUnion.setlTimeBlank(minBlankTime);
					timeUnion.setIntTimeListener(Integer.parseInt(strValue));
				} else if (minBlankTime > timeBlank) {
					minBlankTime = timeBlank;
					timeUnion.setStrTime(strKey);
					timeUnion.setlTimeBlank(minBlankTime);
					timeUnion.setIntTimeListener(Integer.parseInt(strValue));
				}
			}
		} catch (Exception e) {
			//Contants.writeFileToSD("[0007]" + e.toString());
			return null;
		}

		return timeUnion;
	}
}
